<?php
/**
 *  PHP版本的自动生成有规则的订单号(或编号)
 *  作者: 雪浪
 *  日期: 2009-8-14
 *  生成的格式是: 200908010001 前面几位为当前的日期,后面五位为系统自增长类型的编号
 *  原理:
 *      1.获取当前日期格式化值;
 *      2.读取文件,上次编号的值+1最为当前此次编号的值(记录以文件的形式存储)
 *      (下月会接着这个编号)
 */

class FileUtils {

	private $filename;
	//文件名
	private $separate;
	//系统分隔符
	private $width;
	//自动增长部分的个数

	public function __construct($width, $filename, $separate) {

		$this -> width = $width;
		$this -> filename = $filename;
		$this -> separate = $separate;
	}

	public static function getOrUpdateNumber($current, $start) {

		$filename = "fileserno.txt";
		$record = IOUtil::read_content($filename);
		$arr = explode(",", $record);
		if ($current == $arr[0]) {//如果是同一天,则继续增长
			$arr[1]++;
			IOUtil::write_content("$arr[0],$arr[1]", $filename);
			//将新值存入文件中

			return "$arr[0]" . str_pad($arr[1], 6, 0, STR_PAD_LEFT);

		} else {//如果两个日期不一样则重新从起始值开始

			$arr[0] = $current;
			$arr[1] = $start;
			IOUtil::write_content("$arr[0],$arr[1]", $filename);
			//将新值存入文件中

			return "$arr[0]" . str_pad($arr[1], 6, 0, STR_PAD_LEFT);
		}

	}

	public static function getno() {
		$filename = "fileserno.txt";
		$record = IOUtil::read_content($filename);
		$arr = explode(",", $record);
		return (int)$arr[1];
	}

	public static function savepic($filename, $jpg) {

		//每日一个目录
		$today = date("Ymd");
		$dir = "upload/{$today}" . "/";
		FileUtils::createDir($dir);
		file_put_contents($filename, $jpg);

	}

	public static function createDir($path) {
		if (!file_exists($path)) {
			mkdir($path, 0777);
		}
	}

	public static function getSN($prefix = '') {

		$chars = md5(uniqid(mt_rand(), true));
		$uuid = substr($chars, 0, 8);
		$uuid .= substr($chars, 8, 4);
		$uuid .= substr($chars, 12, 4);
		$uuid .= substr($chars, 16, 4);
		$uuid .= substr($chars, 20, 12);
		return $prefix . $uuid;
	}

	public static functiOn ranNo($length) {
		$hash = 'moc-';
		// 文件头, 可以自定义
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$max = strlen($chars) - 1;
		mt_srand((double)microtime() * 1000000);
		for ($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
		return $hash;
	}

	function shorturl($url = '', $prefix = '', $suffix = '') {
		$base32 = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5');

		$hex = md5($prefix . $url . $suffix);
		$hexLen = strlen($hex);
		$subHexLen = $hexLen / 8;
		$output = array();

		for ($i = 0; $i < $subHexLen; $i++) {
			$subHex = substr($hex, $i * 8, 8);
			$int = 0x3FFFFFFF & (1 * ('0x' . $subHex));
			$out = '';
			for ($j = 0; $j < 6; $j++) {
				$val = 0x0000001F & $int;
				$out .= $base32[$val];
				$int = $int>>5;
			}
			$output[] = $out;
		}
		return $output;
	}

	function test() {
		//读取xml
		// $x = spClass('spXml');
		// $f = fopen('config.xml', 'r');
		// while ($data = fread($f, 4096)) {
		// $xml .= $data;
		// }
		// $x -> loadXML($xml);
		// echo $x -> config -> imgserver;
	}

}

class  IOUtil {

	public static function read_content($filename) {

		$handle = fopen($filename, "r");
		$content = fread($handle, filesize($filename));

		return $content;
	}

	public static function write_content($content, $filename) {

		$handle = fopen($filename, "w");
		fseek($handle, 0);
		fwrite($handle, $content);

		return $content;
	}

}
?>